package com.fzh.web.yibai.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzh.web.yibai.app.entity.Article;
import com.fzh.web.yibai.app.mapper.ArticleMapper;
import com.fzh.web.yibai.app.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-04-19
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {


    @Autowired
    private ArticleImgServiceImpl imgService;


    @Override
    public List<Article> findArticle(Integer position, Integer limt, Integer webSizeId) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_id",webSizeId);
        queryWrapper.eq("position",position);
        queryWrapper.orderByAsc("create_time");
        queryWrapper.last("limit "+limt);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<Article> findArticle2(Integer position, Integer pageNo, Integer pageSize, Integer webSizeId) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("site_id",webSizeId);
        queryWrapper.eq("position",position);
        queryWrapper.orderByAsc("create_time");
        queryWrapper.last("limit "+pageNo+","+pageSize);

        List<Article> list = baseMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            for(Article article:list){
                article.setImgList(imgService.findArticleImg(article.getId()));
            }
        }

        return list;
    }


    @Override
    public Article findArticleById(String id) {

        return baseMapper.selectById(id);
    }
}
